package com.cn.qgx428.lazydiary.dialog

import android.app.Dialog
import android.content.Context
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import com.cn.qgx428.lazydiary.R
import com.cn.qgx428.lazydiary.mWidget.PickerScrollView
import com.cn.qgx428.lazydiary.mWidget.Pickers
import kotlinx.android.synthetic.main.dialog_scroll_picker.*

/**
 * Created by qinguanxiong on 2018/1/23.
 */
class ScrollPickerDialog: Dialog  {
    lateinit var mainView : View
    lateinit var mContext : Context
    lateinit var listener : (picker: Pickers) -> Unit
    lateinit var pickers : MutableList<Pickers>
    var seletedData : List<String> = mutableListOf()
    var nowPicker  = Pickers("", "")

    public constructor(context: Context, selectedData: List<String>, f: (picker: Pickers) -> Unit): super(context){
        mContext = context
        this.seletedData = selectedData
        listener = f
        mainView = LayoutInflater.from(mContext).inflate(R.layout.dialog_scroll_picker, null)
        setContentView(mainView)
        // 将对话框铺满，设置在底部
        window.setGravity(Gravity.BOTTOM)
        window.setBackgroundDrawableResource(android.R.color.transparent)
        initView()
    }

    private fun initView() {
        // 准备数据
        pickers = mutableListOf()
        for (i in 0..seletedData.size-1){
            var p = Pickers(seletedData.get(i), i.toString())
            pickers.add(p)
        }
        dialog_scrollPicker_scroll.setData(pickers)
        dialog_scrollPicker_scroll.setOnSelectListener(object : PickerScrollView.OnSelectListener{
            override fun onSelect(pickers: Pickers) {
                nowPicker = pickers
                Log.d("hhhhh", pickers.showContent)
            }
        })

        nowPicker = pickers.get(0)

        dialog_scrollPicker_commit.setOnClickListener({
            listener(nowPicker)
            dismiss()
        })
        dialog_scrollPicker_cancel.setOnClickListener({
            dismiss()
        })
    }
}